﻿using System;
using System.Collections.Generic;
using System.Text;

    public class ByteArrayTool
    {
        private static bool Is_Little_Endian = BitConverter.IsLittleEndian;

        public ByteArrayTool() {}

        /// <summary>
        /// 连接两个byte[] 数组
        /// </summary>
        /// <param name="data1"></param>
        /// <param name="data2"></param>
        /// <returns></returns>
        public static byte[] ConnectBytes(byte[] data1, byte[] data2)
        {
            byte[] nCon = null;
            if (data2 == null)
            {
                nCon = new byte[data1.Length];
                data1.CopyTo(nCon, 0);
            }
            else
            {
                nCon = new byte[data1.Length + data2.Length];
                data1.CopyTo(nCon, 0);
                data2.CopyTo(nCon, data1.Length);
            }
            return nCon;
        }

        /// <summary>
        /// 复制字节数组
        /// </summary>
        /// <param name="des">被复制到的字节数组</param>
        /// <param name="sou">用来复制的字节数组</param>
        /// <param name="start">被复制到的字节数组的开始索引</param>
        /// <param name="length">用来复制的字节数组的长度</param>
        public static void BytesCopy(byte[] des, byte[] sou, int start, int length)
        {
            for (int a = 0; a < length; ++a)
            {
                des[start + a] = sou[a];
            }
        }

        public static void BytesCopy(byte[] des, byte[] sou, int start1, int start2, int length)
        {
            if (des.Length - start1 < length || sou.Length - start2 < length)
            {
                return;
            }
            for (int a = 0; a < length; ++a)
            {
                des[start1 + a] = sou[start2 + a];
            }
        }

        /// <summary>
        /// 字节数组转字符串
        /// </summary>
        /// <param name="byteArray">字节数组</param>
        /// <param name="type">转码类型</param>
        /// <returns></returns>
        public static string BytesToString(byte[] byteArray,string type)
        {
            byte[] tempByte = new byte[byteArray.Length];
            Array.Copy(byteArray,tempByte,byteArray.Length);
            ChangeByteTurn(tempByte);
            return Encoding.GetEncoding(type).GetString(tempByte);
        }
        public static string BytesToString(byte[] byteArray)
        {
            byte[] tempByte = new byte[byteArray.Length];
            Array.Copy(byteArray, tempByte, byteArray.Length);
            ChangeByteTurn(tempByte);
            return Encoding.GetEncoding("UTF-8").GetString(tempByte);
        }

        /// <summary>
        /// 字符串转字节数组
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="type">转码类型</param>
        /// <returns></returns>
        public static byte[] StringToBytes(string str,string type) 
        {
            return Encoding.GetEncoding(type).GetBytes(str);
        }
        public static byte[] StringToBytes(string str)
        {
            return Encoding.GetEncoding("UTF-8").GetBytes(str);
        }

        /// <summary>
        /// int 转byte数组+
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static byte[] intToByte(int i)
        {
            byte[] bt = System.BitConverter.GetBytes(i);
            return bt;
        }

        /// <summary>
        /// byte数组转int
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static int byteToInt(byte[] b)
        {
            ChangeByteTurn(b);
            int n = System.BitConverter.ToInt32(b, 0);
            return n;
        }

        /// <summary>
        /// float 转byte数组+
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static byte[] floatToByte(float i)
        {
            byte[] bt = System.BitConverter.GetBytes(i);
            return bt;
        }

        /// <summary>
        /// byte数组转float
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        public static float byteTofloat(byte[] b)
        {
            ChangeByteTurn(b);
            float n = System.BitConverter.ToSingle(b, 0);
            return n;
        }

        private static void ChangeByteTurn(byte[] byteArr)
        {
            if (Is_Little_Endian)
            {
                Array.Reverse(byteArr);
            }
        }



    }
